package com.lin.spbddo.server.service.dubbo;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lin.spbddo.model.entity.ItemInfo;
import com.lin.spbddo.api.enums.StatusCode;
import com.lin.spbddo.model.mapper.ItemInfoMapper;
import com.lin.spbddo.api.response.BaseResponse;
import com.lin.spbddo.api.service.IDubboItemService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import java.util.List;


@Service(protocol = {"dubbo"}, validation = "true", version = "1.0", timeout = 3000)
@Path("dubboOne")
public class DubboItemService implements IDubboItemService {

    private static final Logger log = LoggerFactory.getLogger(DubboItemService.class);

    @Autowired
    private ItemInfoMapper itemInfoMapper;

    /**
     * 列表查询服务，实际的逻辑
     *
     * @return
     */
    @Override
    @Path("item/list")
    public BaseResponse listItems() {
        BaseResponse response = new BaseResponse(StatusCode.Success);
        try {
            List<ItemInfo> itemInfos = itemInfoMapper.selectAll();
            response.setData(itemInfos);
        } catch (Exception e) {
            log.error("列表查询服务发生异常：", e.fillInStackTrace());
        }
        return response;
    }

    @Override
    @Path("item/page/list")
    public BaseResponse listPageItems(@QueryParam("pageNo") Integer pageNo, @QueryParam("pageSize") Integer pageSize) {
        BaseResponse response = new BaseResponse(StatusCode.Success);
        try {
            PageHelper.startPage(pageNo, pageSize);
            PageInfo<ItemInfo> itemInfoPageInfo = new PageInfo<ItemInfo>(itemInfoMapper.selectAll());
            response.setData(itemInfoPageInfo);
        } catch (Exception e) {
            log.error("列表查询服务发生异常：", e.fillInStackTrace());
        }
        return response;
    }

    @Override
    @Path("item/page/list/params")
    public BaseResponse listPageItemsParams(@QueryParam("pageNo") Integer pageNo, @QueryParam("pageSize") Integer pageSize, @QueryParam("search") String search) {
        BaseResponse response = new BaseResponse(StatusCode.Success);
        try {
            PageHelper.startPage(pageNo, pageSize);
            PageInfo<ItemInfo> itemInfoPageInfo = new PageInfo<ItemInfo>(itemInfoMapper.selectAllByParams(search));
            response.setData(itemInfoPageInfo);
        } catch (Exception e) {
            log.error("列表查询服务发生异常：", e.fillInStackTrace());
        }
        return response;
    }

}
